// InsertSort.cpp : This file contains the 'main' function. Program execution begins and ends there.
//

#include <iostream>

void PrintArray(int inArray[], int length);
void InsertSort(int inArray[], int length) {
    PrintArray(inArray, length);
    //指向数组中每个元素
    for (int i = 1; i < length; i ++) {
        //指向需要比较的元素
        for (int j = 0; j < i; j ++) {
            //进行比较
            if (inArray[i] < inArray[j]) {
                //数组内部位置交换 (往后挪)
                int tempArrayI = inArray[i];

                for (int k = i; k > j; k --) {
                    inArray[k] = inArray[k - 1];
                }

                inArray[j] = tempArrayI;
            }
        }

        PrintArray(inArray, length);
    }
}

void PrintArray(int inArray[], int length) {
    for (int i = 0; i < length; i ++) {
        printf("%d ", inArray[i]);
    }
    printf("\n");
}

int main()
{
    int inArray[] = {4, 3, 5, 1, 9, 6};

    printf("插入排序:\n");
    InsertSort(inArray, 6);
}
